/*
 * Copyright (C) 2007-2008 
 * Project 会聚科技
 * Version 3.00
 * Date 2010-1-10
 * Encoding GB2312
 * Author hcgong@sina.com
 * 
 * History 无
 */
package net.comm.param.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.List;

import org.sakaiproject.db.impl.BasicSqlService;

import net.comm.db.DBFunction;
import net.comm.param.bean.BaseSequenceMgr;
import net.comm.param.db.DbSequenceMgrService;
import net.comm.param.db.SequenceMgrServiceSqlMysql;
import net.comm.util.DbcpPool;
import net.comm.util.StringUtil;

/**
 * 序列号管理类
 * @author k
 * @version 3.0
 * @date 2008-1-10 下午03:21:56
 *
 * TODO 负责系统中各种类型序列号的生成、注销
 */
public class SequenceMgr {
	
	private static char SEQ_FILL_CHAR = '0';
	
	
//	/**
//	 * 产生字符串型序号(
//	 * @param date		日期/年份
//	 * @param refKind	业务类型
//	 * @param seqLen	序列号长度
//	 * @return
//	 * @throws Exception
//	 */
//	public static String generateSeq( String date, 	String refKind ,  int seqLen) throws Exception
//	{
//		int seq = genSequence( date , refKind );
//		return StringUtil.lfillCharater(
//				String.valueOf(seq) , SEQ_FILL_CHAR , seqLen);
//	}
	
	
	/**
	 * 产生整型序号
	 * @param date		日期
	 * @param refKind	业务类型
	 * @return
	 * @throws Exception
	 */
	public static int generateSeq( String date,	String refKind ) throws Exception
	{
		return genSequence( date , refKind );
	}
	
	
//	/**
//	 * 产生交易编号
//
//	 * @param refKind	业务类型
//	 * @return
//	 */
//	public static String generateRefNo( String refKind, int seqLen)
//		throws Exception
//	{
//		String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
//		String year2th = year.substring(2);
//		
//		//序列号业务类型+2位年份+7位的序号
//		StringBuffer seqBuff = new StringBuffer();
////		seqBuff.append(refKind);
//		seqBuff.append(year2th); 
//		
//		//获取序列号
//		String seq = String.valueOf(genSequence(year , refKind ));
//		seqBuff.append(StringUtil.lfillCharater(seq , SEQ_FILL_CHAR , seqLen - 2));
//		
//		return seqBuff.toString();
//	}
	
	/**
	 * 直接从seqctrl表中生成一个序列字符串
	 * @param strSysDate	日期/年份
	 * @param strRefKind	类型
	 * @throws Exception
	 */
	private static int genSequence(String date,	String refKind ) throws Exception
	{
		
		int seq = 0;
		Connection cn  = null;
		Statement stmt = null;
		ResultSet rs   = null;
		try 
		{
			cn = DbcpPool.getConnection();
			cn.setAutoCommit(false);
			
			SequenceMgrServiceSqlMysql servSql = new SequenceMgrServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeyUpdSql();
			String updsql = servSql.getUpdateSql();
			Object[] fields3 = new Object[3];
			
			Object[] fields = new Object[2];
			fields[0] = refKind;
			fields[1] = date;

			List list = bSqlServ.dbRead(cn, sql, fields, new DbSequenceMgrService());
			
			if(list.size() > 0){
				BaseSequenceMgr base = (BaseSequenceMgr)list.get(0);
				
				seq = base.getMaxseq() + 1;
				fields3[0] = seq;
				fields3[1] = refKind;
				fields3[2] = date;
				
				boolean  res = bSqlServ.dbWrite(cn, updsql, fields3);
				if(!res){
					cn.rollback();
					DbcpPool.returnConnection(cn);
					return 0;
				}
			}else{
				seq = 1;
				fields3[0] = refKind;
				fields3[1] = date;
				fields3[2] = seq;
				
				sql = servSql.getInsertSql();
				boolean  res = bSqlServ.dbWrite(cn, sql, fields3);
				if(!res){
					cn.rollback();
					DbcpPool.returnConnection(cn);
					return 0;
				}
			}
			cn.commit();
			
		} catch (Exception e) 
		{
			cn.rollback();
			throw e;
		} finally 
		{
			try {
				DBFunction.closeRS(rs);
				DBFunction.closeStatement(stmt);
			} catch(Exception e) {
				e.printStackTrace();
			}
			DbcpPool.returnConnection(cn);
		}

		return seq;
	}
	
	
}
